package com.itheima;

import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo {
    public  static  void main(String[] args) {
        List<String> names = new ArrayList<String>();
        names.add("张三");
        names.add("张三丰");
        names.add("张无忌");
        names.add("赵敏");
        names.add("周芷若");
        names.add("小昭");

        List<String> list1 = new ArrayList<String>();

        names.forEach(name -> {
            if(name.startsWith("张")) {
                //以张开头
                list1.add(name);
            }
        });
        List<String> list2 = new ArrayList<String>();
        list1.forEach(name -> {
            if(name.length() == 3) {
                //长度为3
                list2.add(name);
            }
        });

        names.forEach(name -> System.out.println(name));
        System.out.println("-----------------");
        list1.forEach(name -> System.out.println(name));
        System.out.println("-----------------");
        list2.forEach(name -> System.out.println(name));
        System.out.println("-----------------");
        //1.获取集合的流对象
        //2.筛选，获取张开头的
        //3.终结方法
        names.stream()
                .filter(s -> s.startsWith("张"))
                .filter(s -> s.length() == 3)
                .forEach(System.out::println);//   .forEach(s ->  System.out.println(s));
    }
    @Test
    public void test1(){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(10);

        List<Integer> list1 = list.stream()
                .filter(integer -> integer % 2 == 0)
                .toList();
                //.collect(Collectors.toList());
        System.out.println(list1);
    }

    @Test
    public void test2(){
    List<String> list = new ArrayList<>();
    list.add("张三,23");
    list.add("张三丰,35");
    list.add("张无忌,23");
    list.add("赵敏,24");
    list.add("周芷若,24");
    list.add("小昭,18");
    //过滤年龄大于等于24的
    Map<Object, Object> map = list.stream()
            .filter(s -> {
                String[] split = s.split(",");
                int age = Integer.parseInt(split[1]);
                return age >= 24;
            }).collect(Collectors.toMap(s -> {
                String[] split = s.split(",");
                return split[0];
            }, s -> {
                String[] split = s.split(",");
                return split[1];
            }));

    map.forEach((k,v) -> System.out.println(k + ":" + v));
    }

    @Test
    public void test3(){
        List<String> list1 = new ArrayList<>();
        list1.add("佟大为");
        list1.add("陈赫");
        list1.add("王宝强");
        list1.add("黄渤");
        list1.add("成龙");
        list1.add("胡歌");

        List<String> list2 = new ArrayList<>();
        list2.add("赵丽颖");
        list2.add("刘诗诗");
        list2.add("古力娜扎");
        list2.add("杨幂");
        list2.add("杨紫");
        list2.add("杨天");
        list2.add("迪丽热巴");

        Stream<String> stream1  =  list1.stream().filter(new Predicate<String>() {
            //筛选，长度为3
            @Override
            public boolean test(String s) {
                return s.length()  == 2;
            }
            //截取，只取前两个
        }).limit(2);

        Stream<String> stream2 = list2.stream().filter(new Predicate<String>() {
            //筛选，以杨开头
            @Override
            public boolean test(String s) {
                return s.startsWith("杨");
            }
        }).skip(1);//跳过第一个

        Stream.concat(stream1,stream2).forEach(System.out::println);
    }

}
